
package com.tools.cleanmaster.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.tools.cleanmaster.CleanApplication;

public class UiUtils {

    private static int mStartAlpha, mStartRed, mStartGreen, mStartBlue;
    private static int mIntervalAlpha, mIntervalRed, mIntervalGreen, mIntervalBlue;
    private static int mStartColor, mToColor;
    private static float sDensity = 0.0F;
    private static int sDensityDpi = 0;

    static public int getScreenWidthPixels(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getMetrics(dm);
        return dm.widthPixels;
    }

    public static int getScreenHeightPixels(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getMetrics(dm);
        return dm.heightPixels;
    }

    public static Point getScreenSizePixels(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getMetrics(dm);
        return new Point(dm.widthPixels, dm.heightPixels);
    }

    public static Bitmap drawable2Bitmap(Drawable drawable) {
        if (drawable == null) {
            return null;
        } else {
            return getResizedBitmap(drawable, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
        }
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * Convert drawable to bitmap
     * 
     * @return null paramDrawable can not get width or height.
     */
    public static Bitmap getResizedBitmap(Drawable paramDrawable, int width, int height) {
        if (paramDrawable == null) {
            return null;
        }

        if (width <= 0) {
            width = paramDrawable.getIntrinsicWidth();
        }

        if (height <= 0) {
            height = paramDrawable.getIntrinsicHeight();
        }
        if (width <= 0 || height <= 0) {
            return null;
        }
        paramDrawable.setBounds(0, 0, width, height);
        Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;
        Bitmap localBitmap = Bitmap.createBitmap(width, height, localConfig);
        Canvas localCanvas = new Canvas(localBitmap);
        paramDrawable.draw(localCanvas);
        return localBitmap;
    }

    public static int dipToPx(int dip) {
        return (int) (dip * getScreenDensity() + 0.5f);
    }

    static public int dipToPx(Context context, int dip) {
        return (int) (dip * getScreenDensity(context) + 0.5f);
    }

    public static float getScreenDensity() {
        if (sDensity != 0F) {
            return sDensity;
        } else {
            return getScreenDensity(CleanApplication.getInstance());
        }
    }

    static public float getScreenDensity(Context context) {
        if (sDensity == 0F) {
            try {
                DisplayMetrics dm = new DisplayMetrics();
                ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                        .getMetrics(dm);
                sDensity = dm.density;
            } catch (Exception e) {
                sDensity = 1.0F;
            }
        }
        return sDensity;
    }

    public static int getScreenDensityDpi() {
        if (sDensityDpi == 0) {
            try {
                DisplayMetrics dm = new DisplayMetrics();
                ((WindowManager) CleanApplication.getInstance().getSystemService(Context.WINDOW_SERVICE))
                        .getDefaultDisplay().getMetrics(dm);
                sDensityDpi = dm.densityDpi;
            } catch (Exception e) {
                sDensityDpi = DisplayMetrics.DENSITY_DEFAULT;
            }
        }
        return sDensityDpi;
    }


    public static int transformColor(int startColor, int toColor, float fraction) {
        if (startColor != mStartColor || toColor != mToColor) {
            mStartColor = startColor;
            mToColor = toColor;

            mStartAlpha = Color.alpha(startColor);
            mStartRed = Color.red(startColor);
            mStartGreen = Color.green(startColor);
            mStartBlue = Color.blue(startColor);

            mIntervalAlpha = Color.alpha(toColor) - mStartAlpha;
            mIntervalRed = Color.red(toColor) - mStartRed;
            mIntervalGreen = Color.green(toColor) - mStartGreen;
            mIntervalBlue = Color.blue(toColor) - mStartBlue;
        }

        int currentAlpha = (int) (mIntervalAlpha * fraction + mStartAlpha);
        int currentRed = (int) (mIntervalRed * fraction + mStartRed);
        int currentGreen = (int) (mIntervalGreen * fraction + mStartGreen);
        int currentBlue = (int) (mIntervalBlue * fraction + mStartBlue);
        return Color.argb(currentAlpha, currentRed, currentGreen, currentBlue);
    }

    public static float dipToPxFloat(int dip) {
        return (float)dip * getScreenDensity() + 0.5F;
    }

    public static int spToPx(float sp) {
        return (int)(sp * getScreenDensity() + 0.5F);
    }
}
